{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "50f2e32d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import xlrd\n",
    "import xlwt\n",
    "# import xlwings as xw\n",
    "import xlsxwriter\n",
    "import openpyxl\n",
    "import pandas as pd\n",
    "\n",
    "import datetime\n",
    "from datetime import timedelta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "50a8e659",
   "metadata": {},
   "outputs": [],
   "source": [
    "weekDic = dict()\n",
    "weekDic[0] = \"一\"\n",
    "weekDic[1] = \"二\"\n",
    "weekDic[2] = \"三\"\n",
    "weekDic[3] = \"四\"\n",
    "weekDic[4] = \"五\"\n",
    "weekDic[5] = \"六\"\n",
    "weekDic[6] = \"七\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "242eb830",
   "metadata": {},
   "outputs": [],
   "source": [
    "path = \"E:\\\\Logs\\\\\"\n",
    "filename = \"工作计划_08.xlsx\"\n",
    "xlsx_path = path + filename"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f43e206a",
   "metadata": {},
   "outputs": [],
   "source": [
    "workbook = xlsxwriter.Workbook(xlsx_path)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8fa3e39e",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'NoneType' object has no attribute 'add_format'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[15], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m headtitle \u001b[38;5;241m=\u001b[39m \u001b[43mworkbook\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43madd_format\u001b[49m({\n\u001b[0;32m      2\u001b[0m \n\u001b[0;32m      3\u001b[0m     \u001b[38;5;124m'\u001b[39m\u001b[38;5;124malign\u001b[39m\u001b[38;5;124m'\u001b[39m:\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mright\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[0;32m      4\u001b[0m \n\u001b[0;32m      5\u001b[0m     \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mvalign\u001b[39m\u001b[38;5;124m'\u001b[39m:\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mvcenter\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m      6\u001b[0m \n\u001b[0;32m      7\u001b[0m })\n\u001b[0;32m      9\u001b[0m row_title \u001b[38;5;241m=\u001b[39m workbook\u001b[38;5;241m.\u001b[39madd_format({\n\u001b[0;32m     10\u001b[0m \n\u001b[0;32m     11\u001b[0m     \u001b[38;5;124m'\u001b[39m\u001b[38;5;124malign\u001b[39m\u001b[38;5;124m'\u001b[39m:\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mcenter\u001b[39m\u001b[38;5;124m'\u001b[39m,\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m     14\u001b[0m \n\u001b[0;32m     15\u001b[0m })\n\u001b[0;32m     17\u001b[0m title \u001b[38;5;241m=\u001b[39m workbook\u001b[38;5;241m.\u001b[39madd_format({\n\u001b[0;32m     18\u001b[0m \n\u001b[0;32m     19\u001b[0m     \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mbold\u001b[39m\u001b[38;5;124m'\u001b[39m: \u001b[38;5;28;01mTrue\u001b[39;00m,\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m     24\u001b[0m \n\u001b[0;32m     25\u001b[0m     \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mfont_color\u001b[39m\u001b[38;5;124m'\u001b[39m:\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m#FEFEFE\u001b[39m\u001b[38;5;124m'\u001b[39m})\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'add_format'"
     ]
    }
   ],
   "source": [
    "headtitle = workbook.add_format({\n",
    "\n",
    "    'align':'right',\n",
    "\n",
    "    'valign':'vcenter'\n",
    "\n",
    "})\n",
    "\n",
    "row_title = workbook.add_format({\n",
    "\n",
    "    'align':'center',\n",
    "\n",
    "    'valign':'vcenter'\n",
    "\n",
    "})\n",
    "\n",
    "title = workbook.add_format({\n",
    "\n",
    "    'bold': True,\n",
    "\n",
    "    'align':'center',\n",
    "\n",
    "    'bg_color':'#1F900F',          #表格背景颜色\n",
    "\n",
    "    'font_color':'#FEFEFE'})\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e2c57181",
   "metadata": {},
   "outputs": [],
   "source": [
    "now = datetime.datetime.now()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6f4cb92d",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "date_string = f\"{now.year}-{now.month+1}-01\"\n",
    "start_month = datetime.datetime.strptime(date_string, \"%Y-%m-%d\")\n",
    "end_month = datetime.datetime.strptime(f\"{now.year}-{now.month + 2}-01\", \"%Y-%m-%d\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "1174464f",
   "metadata": {},
   "outputs": [],
   "source": [
    "workbook = xlsxwriter.Workbook(xlsx_path)\n",
    "\n",
    "worksheet = workbook.add_worksheet()\n",
    " \n",
    "\n",
    "bold = workbook.add_format({'bold': True,\n",
    "\n",
    "    'valign':'vcenter',\n",
    "\n",
    "    'align':'center'})\n",
    "\n",
    "style_head = workbook.add_format({'bold': True,\n",
    "\n",
    "    'valign':'left',\n",
    "\n",
    "    'align':'top'})\n",
    " \n",
    "\n",
    "# 注意：XlsxWriter只能创建文件，不能读取或修改现有的文件。\n",
    "\n",
    "# 第一行\n",
    "row = 1\n",
    "worksheet.merge_range(f\"A{row}:G{row}\", f\"{start_month.month}月打卡表\", bold)\n",
    "worksheet.set_row(0, 50)\n",
    "\n",
    "\n",
    "worksheet.merge_range(f\"H{row}:AG{row}\", \"*本月主要目标\", style_head)   \n",
    "\n",
    "# 第二行\n",
    " \n",
    "row = 2\n",
    "worksheet.merge_range(f\"B{row}:G{row}\", \"第一周\", title)   \n",
    "\n",
    "worksheet.merge_range(f\"H{row}:N{row}\", \"第二周\", title)   \n",
    "\n",
    "worksheet.merge_range(f\"O{row}:U{row}\", \"第三周\", title)   \n",
    "\n",
    "worksheet.merge_range(f\"V{row}:AB{row}\", \"第四周\", title)\n",
    "\n",
    " \n",
    "\n",
    "#worksheet.merge_range(\"A2:A3\", \"\", headtitle)   \n",
    "\n",
    "index_date = start_month\n",
    "\n",
    "worksheet.write(2,0,\"星期\" , bold)\n",
    "\n",
    "worksheet.write(3,0,\"日期\" , bold)\n",
    "\n",
    "   \n",
    "\n",
    "worksheet.set_column(1, 1, 5)\n",
    "worksheet.set_column(0, 0, 25)\n",
    "\n",
    "       \n",
    "\n",
    "while index_date >= start_month and index_date < end_month:\n",
    "\n",
    "    worksheet.write(2, index_date.day ,weekDic[index_date.weekday()] , headtitle)\n",
    "\n",
    "    worksheet.write(3, index_date.day ,index_date.day, headtitle)\n",
    "\n",
    "   \n",
    "\n",
    "    worksheet.set_column(2, index_date.day, 2)\n",
    "\n",
    "   \n",
    "\n",
    "    index_date = index_date + timedelta(days=1)\n",
    "     \n",
    "worksheet.write(4, 0 ,\"读经\" , bold)\n",
    "worksheet.write(5, 0 ,\"八段锦\" , bold)\n",
    "worksheet.write(6, 0 ,\"马步\" , bold)\n",
    "worksheet.write(7, 0 ,\"打坐\" , bold)\n",
    "\n",
    "for i in range(4,22):\n",
    "    worksheet.set_row(i, 18)\n",
    "\n",
    "# 调整列宽。\n",
    "\n",
    "row = 21\n",
    "\n",
    "\n",
    "style_foot = workbook.add_format({'bold': True,\n",
    "\n",
    "    'valign':'center',\n",
    "\n",
    "    'align':'right'})\n",
    "\n",
    "worksheet.write(20, 0 ,\"总结\" , style_foot)\n",
    "\n",
    "worksheet.merge_range(f\"B{row}:AF{row+1}\", \"\", bold)\n",
    "   \n",
    "\n",
    "workbook.close()\n",
    "\n",
    "workbook = None\n",
    "\n",
    " \n",
    "\n",
    " \n",
    "\n",
    " \n",
    "\n",
    " \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "9003d47f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "5\n",
      "6\n"
     ]
    }
   ],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
